<template>
	<uni-list>
		<uni-list-item title="头像" link @click="updateAvatar">
			<template v-slot:footer>
				<image :src="userInfo.avatar" style="width: 80rpx;" mode="widthFix"></image>
			</template>
		</uni-list-item>
		<uni-list-item title="昵称" link :right-text="userInfo.nickName" @click="updateNick"></uni-list-item>
	</uni-list>
</template>

<script>
	import store from '../../../store/index.js'
	import { pathToBase64 } from 'image-tools'
	const unicloudUser = uniCloud.importObject('user')
	export default {
		computed: {
			userInfo() {
				return store.state.user.userInfo
			}
		},
		methods: {
			updateAvatar() {
				const that = this
				uni.chooseImage({
					count: 1,
					success(res) {
						const path = res.tempFilePaths[0]
						uniCloud.uploadFile({
							filePath:path,
							cloudPath: `${that.userInfo.nickName}_${new Date().getTime()}`,
							success(ret) {
								unicloudUser.setUser({ uid: that.userInfo.uid, avatar: ret.fileID }).then(res => {
									return unicloudUser.getUser()
								}).then(res => {
									uni.showToast({
										title: '操作成功'
									})
									store.commit('user/SET_USERINFO', res)
									that.$forceUpdate()
								})
							}
						})
					}
				})
			},
			updateNick() {
				const that = this
				uni.showModal({
					title: '修改昵称',
					editable: true,
					content: that.userInfo.nickName,
					success(res) {
						if(res.confirm) {
							unicloudUser.setUser({ uid: that.userInfo.uid, nickName:res.content }).then(res => {
								return unicloudUser.getUser()
							}).then(res => {
								uni.showToast({
									title: '操作成功'
								})
								store.commit('user/SET_USERINFO', res)
								that.$forceUpdate()
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>